//
//  searchViewController.m
//  LubanWork
//
//  Created by xiaohao on 2020/1/7.
//  Copyright © 2020 xiaohao. All rights reserved.
//

#import "searchViewController.h"
#import "SearchBarDisplayCenter.h"
#import "SearchCollectionViewCell.h"
#import "CollectionDetailViewController.h"
@interface searchViewController ()<UITextFieldDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) UICollectionView * main_Clv;
@property(nonatomic,strong) NSArray * titleArray;
@property (nonatomic,strong)UISearchBar *searchBar;

@end

static NSString *cellClvID = @"SearchCollectionViewCell";
@implementation searchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleArray = @[@"在家可做",@"高薪",@"临时工",@"日结",@"周末双休",@"暑假工"];
    [self  setNav];
    [self.view addSubview:self.main_Clv];

    
    
}
-(void)setNav{
    UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 275, 35)];
    ViewRadius(backview, 5);
    backview.backgroundColor = kColorFromRGBHex(0xF1EEFF);
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 15, 15)];
    img.image = [UIImage imageNamed:@"souso"];
    [backview addSubview:img];
    
    UITextField *searchField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 275-37, 35)];
    ViewRadius(searchField, 5);
    searchField.backgroundColor = kColorFromRGBHex(0xF1EEFF);
    searchField.textColor= kColorFromRGBHex(0x666666);
    
    searchField.font= kFONT(11);
    
    //    UIImage *img =;
    //    searchField.leftView = [UIImageView]
//    UIImageView *imageViewUserName=[[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 30, 30)];
//    imageViewUserName.image=[UIImage imageNamed:@"souso"];
//    searchField.leftView.frame = CGRectMake(5, 0, 30, 30);
//    searchField.leftView=imageViewUserName;
//
    searchField.delegate = self;
    searchField.leftViewMode=UITextFieldViewModeAlways; //此处用来设置leftview现实时机
    NSAttributedString* place23 = [[NSAttributedString alloc] initWithString:@"搜索你要的兼职" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:168/255.0f green:168/255.0f blue:168/255.0f alpha:1],NSFontAttributeName:kFONT(11)}];
    searchField.attributedPlaceholder = place23;
    
    searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [backview addSubview:searchField];
    self.navigationItem.titleView = backview ;
}
-(void)getSearchKeyWord:(NSString *)searchWord{
    
}
-(UICollectionView *)main_Clv{
    if (!_main_Clv) {
        UICollectionViewFlowLayout *layo = [[UICollectionViewFlowLayout alloc] init];
        layo.minimumLineSpacing = 5;
        layo.minimumInteritemSpacing = 0;
        layo.itemSize = CGSizeMake((WIDTH-30)/2, 45);
        layo.headerReferenceSize = CGSizeMake(WIDTH, 30);
        _main_Clv = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0, WIDTH-30, HEIGHT-kNavHeight) collectionViewLayout:layo];
        _main_Clv.backgroundColor = kColorFromRGB(249, 247, 255);
        _main_Clv.delegate = self;
        _main_Clv.dataSource = self;
        [_main_Clv registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:cellClvID];
        [_main_Clv registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ViewCollectionViewHeader"];
        
    }
    return _main_Clv;
}
#pragma --mark clv delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.titleArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellClvID forIndexPath:indexPath];
    cell.titLab.text = self.titleArray[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CollectionDetailViewController *CollectionDetailVC = [[CollectionDetailViewController alloc] init];
    CollectionDetailVC.isNav = YES;
    CollectionDetailVC.name = self.titleArray[indexPath.row];
    CollectionDetailVC.navString = @"搜索结果";
    [self.navigationController pushViewController:CollectionDetailVC animated:YES];
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    CollectionDetailViewController *CollectionDetailVC = [[CollectionDetailViewController alloc] init];
      CollectionDetailVC.isNav = YES;
      CollectionDetailVC.name = textField.text;
      CollectionDetailVC.navString = @"搜索结果";
      [self.navigationController pushViewController:CollectionDetailVC animated:YES];
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    // 视图添加到 UICollectionReusableView 创建的对象中
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ViewCollectionViewHeader" forIndexPath:indexPath];
        
        UILabel *label = [UILabel CreatLabeltext:@"热门搜索" Font:MarkFont(12) Textcolor:kColorFromRGBHex(0x666666) textAlignment:NSTextAlignmentLeft];
        [headerView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(headerView);
            make.left.mas_offset(0);
        }];
        return headerView;

    }
    
    return nil;
        
}
@end
